package com.ruoyi.project.er.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.er.domain.PaeaErPoint;
import com.ruoyi.project.er.domain.PaeaErTypePointRel;
import com.ruoyi.project.er.mapper.PaeaErPointMapper;
import com.ruoyi.project.er.model.PaeaErPointModel;
import com.ruoyi.project.er.service.IPaeaErPointService;
import com.ruoyi.project.er.domain.PaeaErType;
import com.ruoyi.project.er.service.IPaeaErTypeService;
import com.ruoyi.project.oms.paeaAuType.domain.PaeaAuType;
import com.ruoyi.project.oms.paeaAuType.service.IPaeaAuTypeService;
import com.ruoyi.project.oms.paeaAuTypeErrPointRel.domain.PaeaAuTypeErrPointRel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 风险点定义Service业务层处理
 *
 * @author ruoyi
 * @date 2020-03-13
 */
@Service
public class PaeaErPointServiceImpl extends ServiceImpl<PaeaErPointMapper, PaeaErPoint> implements IPaeaErPointService
{
    @Resource
    private PaeaErPointMapper paeaErPointMapper;
    @Resource
    private IPaeaErTypeService paeaErTypeService;
    @Resource
    private IPaeaAuTypeService paeaAuTypeService;
    /**
     * 查询风险点定义
     *
     * @param id 风险点定义ID
     * @return 风险点定义
     */
    @Override
    public PaeaErPoint selectPaeaErPointById(String id)
    {
        return paeaErPointMapper.selectPaeaErPointById(id);
    }

    /**
     * 查询风险点定义列表
     *
     * @param paeaErPoint 风险点定义
     * @return 风险点定义
     */
    @Override
    public List<PaeaErPoint> selectPaeaErPointList(PaeaErPoint paeaErPoint)
    {
        return paeaErPointMapper.selectPaeaErPointList(paeaErPoint);
    }

    /**
     * 新增风险点定义
     *
     * @param paeaErPoint 风险点定义
     * @return 结果
     */
    @Override
    public int insertPaeaErPoint(PaeaErPoint paeaErPoint)
    {
        return paeaErPointMapper.insertPaeaErPoint(paeaErPoint);
    }

    /**
     * 修改风险点定义
     *
     * @param paeaErPoint 风险点定义
     * @return 结果
     */
    @Override
    public int updatePaeaErPoint(PaeaErPoint paeaErPoint)
    {
        return paeaErPointMapper.updatePaeaErPoint(paeaErPoint);
    }

    /**
     * 删除风险点定义对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaErPointByIds(String ids)
    {
        return paeaErPointMapper.deletePaeaErPointByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除风险点定义信息
     *
     * @param id 风险点定义ID
     * @return 结果
     */
    @Override
    public int deletePaeaErPointById(String id)
    {
        return paeaErPointMapper.deletePaeaErPointById(id);
    }
    /**
     *查询风险点
     * @param model 角色对象
     * @return 风险点树
     */
    @Override
    public List<Ztree> pointTreeData(PaeaErPointModel model)
    {
        String paeaAuTypeId = model.getAuTypeId();
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<PaeaErPoint> paeaErPoints= new ArrayList();
        if (StringUtils.isNotNull(paeaAuTypeId))
        {
            paeaErPoints = paeaErPointMapper.selectPaeaErPointsByAuTypeId(model);
        }
        if(paeaErPoints==null || paeaErPoints.size()==0){//如果未配置差错点则按照该类显示
            paeaErPoints = paeaErPointMapper.selectPaeaErPointsByErrSource(model);
        }
        ztrees = initZtree(paeaErPoints);
        return ztrees;
    }



    /**
     * 对象转风险点树
     *
     * @param pointList 风险点列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<PaeaErPoint> pointList)
    {
        return initZtree(pointList,null);
    }

    /**
     * 对象转部门树
     *
     * @param pointList 部门列表
     * @param errPointList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<PaeaErPoint> pointList,  List<String> errPointList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(errPointList);
        //差错类型列表，去重复
        Set<String> paeaErTypeIds =new HashSet<String>();
        for (PaeaErPoint point : pointList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(point.getId());
            ztree.setpId("EPI"+point.getErrTypeId());
            ztree.setName(point.getCode()+"-"+point.getName());
            ztree.setTitle(point.getName());
            paeaErTypeIds.add(point.getErrTypeId());
            if (isCheck)
            {
                ztree.setChecked(errPointList.contains(point.getId() + point.getName()));
            }
            ztrees.add(ztree);

        }
        PaeaErType paeaErType=null;
        for(String paeaErTypeId :paeaErTypeIds){
            Ztree ztree = new Ztree();
            paeaErType= paeaErTypeService.selectPaeaErTypeById(paeaErTypeId);
            ztree.setId("EPI"+paeaErTypeId);
            ztree.setpId("0");
            ztree.setName(paeaErType.getCode()+"-"+paeaErType.getTypeName());
            ztree.setTitle(paeaErType.getTypeName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    @Override
    public List<PaeaErPoint> selectPaeaErPointByErrId(String errId) {
        return paeaErPointMapper.selectPaeaErPointsByErrId(errId);
    }
}
